package com.example;

import org.junit.Test;

import java.io.*;

/**
 * @ClassName SerializeTest
 * @Description TODO
 * @Author: XMK
 * @Date: 2021/9/9 14:31
 * @Version: 1.0
 */
public class SerializeTest {

    @Test
    public void test1() throws Exception {

        SubC s = new SubC();
        s.setI1(22);
        s.setP2("p2");
        s.setP1("p1");

        System.out.println(s.getP1());

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://aalog/aa.txt"))) {
            oos.writeObject(s);
            oos.writeObject(s);
        }
    }

    @Test
    public void test2() throws Exception {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://aalog/aa.txt"))) {
            SubC p1 = (SubC)ois.readObject();
            SubC p2 = (SubC)ois.readObject();

            /**
             * 如果父类没有实现serializable接口，那么 反序列化的时候，会调父类的无参构造函数，父类的实例变量默认的初始值
             */
            System.out.println(p1.getP1());
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p2 == p1);
        }


    }

    @Test
    public void writeObject() throws Exception {
        Person p1 = new Person("aa", 12);
        Person p2 = new Person("bb", 20);
        Person p3 = new Person("bb", 20);

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://aalog/aa.txt"))) {
            oos.writeObject(p1);
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
        }
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://aalog/bb.txt"))) {
            oos.writeObject(p1);
        }
    }

    @Test
    public void readObejct() throws Exception {

        Person p1 = null;
        Person p5 = null;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://aalog/aa.txt"))) {
            p1 = (Person)ois.readObject();
            Person p2 = (Person)ois.readObject();
            Person p3 = (Person)ois.readObject();
            Person p4 = (Person)ois.readObject();

            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p2 == p1);
            System.out.println(p3);
            System.out.println(p4);
            System.out.println(p3 == p4);
        }
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://aalog/bb.txt"))) {
            p5 = (Person)ois.readObject();
        }
        System.out.println(p5);
        System.out.println(p1 == p5);
    }


    @Test
    public void writeObject2() throws Exception {

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://aalog/aa.txt"))) {
            Person p1 = new Person("aa", 12);
            oos.writeObject(p1);
            p1.setName("bbbbbbbbbbbb");
            oos.writeObject(p1);
        }
    }

    @Test
    public void readObejct2() throws Exception {

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D://aalog/aa.txt"))) {
            Person p1 = (Person)ois.readObject();
            Person p2 = (Person)ois.readObject();

            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p2 == p1);
            System.out.println(p1.getName().equals(p2.getName()));
        }
    }
}
